package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.common.ResultData;
import com.itheima.domain.SmsHomeBrand;
import com.itheima.service.SmsHomeBrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Morty
 * @create 2022 11 12 19:06
 */
@RestController
@CrossOrigin(origins = "*")
public class SmsHomeBrandController {

    @DubboReference
    SmsHomeBrandService homeBrandService;

    //分页查询推荐品牌
    @GetMapping("/home/brand/list")
    public ResultData pageList(String brandName,Integer pageNum,Integer pageSize,Integer recommendStatus){
        PageResult pageResult = homeBrandService.pageList(brandName,pageNum,pageSize,recommendStatus);
        return ResultData.ok(pageResult,"");
    }
    //添加推荐品牌
    @PostMapping("/home/brand/create")
    public ResultData save(@RequestBody List<SmsHomeBrand> brandList){
        homeBrandService.saveBatch(brandList);
        return ResultData.ok("","");
    }
    //批量删除推荐品牌
    @PostMapping("/home/brand/delete")
    public ResultData deleteByIds(@RequestParam List<Integer> ids){
        homeBrandService.removeByIds(ids);
        return ResultData.ok("","");
    }
    //批量修改推荐品牌状态
    @PostMapping("/home/brand/update/recommendStatus")
    public ResultData updateStatusByIds(@RequestParam List<Integer> ids,Integer recommendStatus){
        for (Integer id : ids) {
            SmsHomeBrand homeBrand = homeBrandService.getById(id);
            homeBrand.setRecommendStatus(recommendStatus);
            homeBrandService.updateById(homeBrand);
        }
        return ResultData.ok("","");
    }
    //修改品牌状态排序
    @PostMapping("/home/brand/update/sort/{id}")
    public ResultData updateSortById(@PathVariable Integer id, Integer sort){
        SmsHomeBrand homeBrand = homeBrandService.getById(id);
        if (sort != null){
            homeBrand.setSort(sort);
        }
        homeBrandService.updateById(homeBrand);
        return ResultData.ok("","");
    }
}
